function myNew() {
  let constructor = Array.from(arguments).shift();
  let args = Array.from(arguments).slice(1, -1);
  let newObj = {};
  newObj.__proto__ = constructor.prototype;
  if (!(typeof constructor === "function")) {
    console.log("error");
  }

  let res = constructor.call(newObj, ...args);
  return res ? res : newObj;
}

function myNew(fn, ...args) {
  // 1. 创建一个空对象
  let obj = Object.create(fn.prototype);
  // 2. 调用构造函数，绑定this，并传入参数
  let result = fn.apply(obj, args);
  // 3. 如果构造函数返回了一个新的对象，则返回该对象；否则返回步骤1创建的对象
  return result instanceof Object ? result : obj;
}

console.log(
  myNew(function () {
    this.a = 1;
  })
);
